package org.gpp.proj1.robot.action.step;

import java.util.List;
import java.util.Set;

import org.gpp.proj1.particle.ParticleCreator;
import org.gpp.proj1.robot.Robot;
import org.gpp.proj1.robot.part.EquippedPart;
import org.gpp.proj1.robot.part.emissor.Emissor;

import com.jme.scene.Node;
import com.jmex.effects.particles.ParticleGeometry;

public class EmissionActionStep implements ActionStep {
	
	private String emissorName;
	private String particle;
	
	public EmissionActionStep( String emissorName, String particle ) {
		this.emissorName = emissorName;
		this.particle = particle;
	}

	public void execute( EquippedPart part, List<Robot> targets ) {
		this.execute( part );
	}
	
	public String getEmissorName() {
		return emissorName;
	}

	/* (non-Javadoc)
	 * @see org.gpp.proj1.robot.action.step.ActionStep#execute(org.gpp.proj1.robot.part.EquippedPart)
	 */
	public void execute(EquippedPart parent) {
		
		Emissor emissor = parent.getEmissor( emissorName );
		Set<String> bones = parent.getAssignedBoneGroupNames();
		
		// For each bone on the emissor parent list, attach it to the bone
		// and start the particle emission
		for( String boneName : bones ) {
			Node emissorNode = emissor.getEmissorNode( boneName );
			
			
			if( emissorNode != null ) {				
				parent.getParentRobot().attachEmissor( boneName, emissor );
				
				ParticleGeometry particles = ParticleCreator.createParticle( particle );
				particles.forceRespawn();
				
				particles.setRotateWithScene( true );
				
				emissorNode.attachChild( particles );
			}
		}
	}
}